package com.share.one.socket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Demo3_MoreThread {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws Exception {
		demo1();
		
		
//		//发送线程
//		System.out.println("启动发送线程");
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				try {
//					DatagramSocket socket = new DatagramSocket();
//					Scanner sc = new Scanner(System.in);
//					while (true) {
//						String line = sc.nextLine();
//						if ("quit".equals(line)) {
//							break;
//						}
//						DatagramPacket packet = new DatagramPacket(line.getBytes(), line.getBytes().length, 
//								InetAddress.getByName("127.0.0.1"), 6667);
//						System.out.println(Thread.currentThread().getName()+ "发送");
//						socket.send(packet);
//					}
//					socket.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}).start();
//
//		Thread.sleep(5000);
//
//		//接收线程
//		System.out.println("开启接收线程");
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				try {
//					DatagramSocket socket = new DatagramSocket(6667);
//					DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
//					while (true) {
//						socket.receive(packet);
//						System.out.println(Thread.currentThread().getName() + "接收");
//
//						byte[] data = packet.getData();
//						int length = packet.getLength();
//						String hostAddress = packet.getAddress().getHostAddress();
//						int port = packet.getPort();
//
//						System.out.println(hostAddress + ":" + port + new String(data, 0, length));
//
//					}
//
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//
//			}
//		}).start();



	}

	private static void demo1() {
		new Receive().start();

		new Send().start();
	}

}

class Receive extends Thread {
	public void run() {
		try {
			DatagramSocket socket = new DatagramSocket(6666);		//创建Socket相当于创建码头
			DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);//创建Packet相当于创建集装箱

			while(true) {
				socket.receive(packet);									//接货,接收数据

				byte[] arr = packet.getData();							//获取数据
				int len = packet.getLength();							//获取有效的字节个数
				String ip = packet.getAddress().getHostAddress();		//获取ip地址
				int port = packet.getPort();							//获取端口号
				System.out.println(ip + ":" + port + ":" + new String(arr,0,len));
			}
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}

class Send extends Thread {
	public void run() {
		try {
			Scanner sc = new Scanner(System.in);						//创建键盘录入对象
			DatagramSocket socket = new DatagramSocket();				//创建Socket相当于创建码头

			while(true) {
				String line = sc.nextLine();							//获取键盘录入的字符串
				if("quit".equals(line)) {
					break;
				}
				DatagramPacket packet = 								//创建Packet相当于集装箱
						new DatagramPacket(line.getBytes(), line.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
				socket.send(packet);									//发货,将数据发出去
			}
			socket.close();
		}  catch (IOException e) {

			e.printStackTrace();
		}		
	}
}